Image Tools


Adaptive Fourier Filtering

part of

pyTEMlib, a pycroscopy library

Notebook by

Gerd Duscher

Materials Science & Engineering Joint Institute of Advanced Materials The University of Tennessee, Knoxville

An introduction into Fourier Filtering of images.

Install pyTEMlib

If you have not done so in the Introduction Notebook, please test and install pyTEMlib and other important packages with the code cell below.

[32]:
import sys
from IPython.lib.deepreload import reload as dreload

try:
    import sidpy
except ModuleNotFoundError:
    !pip3 install sidpy
if sidpy.__version__ < '0.0.3':
    !{sys.executable} -m pip install  --upgrade sidpy
    dreload(sidpy)
try:
    import pyNSID
except ModuleNotFoundError:
    !{sys.executable} -m pip install  --upgrade pyNSID
try:
    import pyTEMlib
except ModuleNotFoundError:
    !{sys.executable} -m pip install  --upgrade pyTEMlib
if pyTEMlib.__version__ < '0.2020.10.3':
    !{sys.executable} -m pip install  --upgrade pyTEMlib
    dreload(pyTEMlib)
from IPython.lib.deepreload import reload as dreload

import sys
!{sys.executable} -m pip install  --upgrade ""../dist/pyTEMlib-0.2020.10.3-py3-none-any.whl"
dreload(pyTEMlib)
sidpy version:  0.0.3

Loading of necessary libraries

Please note, that we only need to load the pyTEMlib library, which is based on sidpy Datsets.

[2]:
%pylab --no-import-all notebook
%gui qt

import pyTEMlib
import pyTEMlib.file_tools as ft
import pyTEMlib.image_tools as it
print('pyTEMlib version: ', pyTEMlib.__version__)
Populating the interactive namespace from numpy and matplotlib
pyTEMlib version:  0.2020.10.3

Open File

These datasets are stored in the pyNSID data format (extension: hf5) automatically.

All results can be stored in that file.

[7]:
try:
    dataset.h5_dataset.file.close()
except:
    pass
dataset= ft.open_file()
if dataset.data_type.name != 'IMAGE':
    print('We really would need an image here')
dataset.plot()
<HDF5 group "/Measurement_000/Channel_000" (0 members)> 05_SI Survey Image
[8]:
choose_image = ft.ChooseDataset(dataset.h5_dataset.parent)
C:\Users\gduscher\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:87: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
[9]:
choose_image.dataset
[9]:
Array Chunk
Bytes 1.05 MB 1.05 MB
Shape (512, 512) (512, 512)
Count 1 Tasks 1 Chunks
Type uint32 numpy.ndarray
512 512

Fourier Transform of Image

[38]:
fft_dataset = it.fourier_transform(choose_image.dataset)
fft_dataset.view_metadata()
kwargs = {'figsize':(10,5)}
fft_dataset.plot(kwargs)
Shape of dataset is:  (512, 512)
2D dataset

Power Spectrum of Image

[18]:
power_spectrum = it.power_spectrum(choose_image.dataset, smoothing=1)

power_spectrum.view_metadata()
print('source: ', power_spectrum.source)
power_spectrum.plot()
smoothing : 1
source:  /Measurement_000/Channel_000/05_SI Survey Image

Spot Detection in Fourier Transform

[29]:
spots = it.diffractogram_spots(power_spectrum, spot_threshold=0.09)
power_spectrum.plot()
plt.gca().scatter(spots[:,1],spots[:,0], color='red', alpha=0.4);
Found 19 reflections

Adaptive Fourier Filter

[34]:
filtered_dataset = it.adaptive_fourier_filter(choose_image.dataset, spots, low_pass=2., reflection_radius=.3)
filtered_dataset.plot()

Let’s see what we did - In Fourier space, of course.

[35]:
filtered_power_spectrum = it.power_spectrum(filtered_dataset, smoothing=0)

power_spectrum.view_metadata()
print('source: ', power_spectrum.source)
filtered_power_spectrum.plot()
smoothing : 1
source:  /Measurement_000/Channel_000/05_SI Survey Image

Please note that the spots are ordered from center to outside.

The third parameter of a spot is its angle.

[48]:
print(spots[:5])
[[ 0.          0.          0.        ]
 [-1.8125      1.5        -0.8794594 ]
 [ 1.8125     -1.5         2.26213326]
 [ 1.5         2.          0.64350111]
 [-1.5        -2.         -2.49809154]]

Log the result

[39]:
results_channel = ft.log_results(dataset.h5_dataset.parent, filtered_dataset)
<HDF5 group "/Measurement_000/Channel_000/Log_000" (0 members)> Fourier filtered 05_SI Survey Image

A tree-like plot of the file

[40]:
ft.h5_tree(dataset.h5_dataset.file)
/
├ Measurement_000
  ---------------
  ├ Channel_000
    -----------
    ├ 05_SI Survey Image
    ├ Log_000
      -------
      ├ Fourier filtered 05_SI Survey Image
      ├ metadata
        --------
      ├ original_metadata
        -----------------
      ├ x
      ├ y
    ├ metadata
      --------
    ├ original_metadata
      -----------------
    ├ x
    ├ y

A convenient function to select a dataset (for further processing, visualization or whatever)

[41]:
choose_image = ft.ChooseDataset(dataset.h5_dataset.parent)
C:\Users\gduscher\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:87: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

The selected dataset can then easily be plotted

[43]:
choose_image.dataset.plot()

Close File

let’s close the file but keep the filename

[44]:
filename = results_channel.file.filename
results_channel.file.close()

Simulate new notebook

We can now simulate a new notebook and open the file again.

[45]:
new_dataset= ft.open_file(filename)
choose_image = ft.ChooseDataset(new_dataset.h5_dataset.parent)

C:\Users\gduscher\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:87: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
[46]:
choose_image.dataset.plot()

We want to make an image operation of the images in the file.

[54]:
choose_image = ft.ChooseDataset(new_dataset.h5_dataset.parent.parent)
print('       subtract')
choose_image2 = ft.ChooseDataset(new_dataset.h5_dataset.parent)
       subtract
[55]:
new_image = np.array(choose_image.dataset) - np.array(choose_image2.dataset)
new_image = new_dataset.like_data(new_image)
new_image.plot()

Close File for Good

[56]:
new_dataset.h5_dataset.file.close()
[ ]: